package com.youdoneed.community.shortVideo.web;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.youdoneed.community.common.vo.PageData;
import com.youdoneed.community.common.vo.ResultMessage;
import com.youdoneed.community.shortVideo.entity.Musiclibrary;
import com.youdoneed.community.shortVideo.service.MusiclibraryService;
import com.youdoneed.community.shortVideo.vo.BatchVo;
import com.youdoneed.community.shortVideo.vo.MusicBatchVo;
import com.youdoneed.community.shortVideo.vo.MusiclibraryTypeVo;
import com.youdoneed.community.shortVideo.vo.RecommendVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 音乐管理模块控制器
 * @author lixs
 * 2017年8月31日
 */
@SuppressWarnings("unchecked")
@RestController
@RequestMapping(value = "/music", method = {RequestMethod.GET, RequestMethod.POST})
@Api(value = "音乐模块控制器", description = "音乐模块控制器")
public class MusiclibraryController {

	private final Logger logger = LoggerFactory.getLogger(MusiclibraryController.class);
	
	@Autowired
	MusiclibraryService musiclibraryService; 
	
	/**
	 * 向音乐库添加音乐
	 * @param musiclibrary
	 * @author lixs
	 * @Date 2017.8.31
	 */
	@ResponseBody
    @RequestMapping(value = "/addMusicToLibrary", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "向音乐库添加音乐", value = "向音乐库添加音乐")
	public ResultMessage<Integer> addMusicToLibrary(@RequestBody MusiclibraryTypeVo vo){
		logger.info("向音乐库添加音乐");
//		String[] typeId = vo.getTypeId();
//		String[] typeId = typeIds.toArray(new String[typeIds.size()]);
		Musiclibrary musiclibrary = vo.getMusiclibrary();
		List<String> typeId = vo.getTypeId();
		int result = musiclibraryService.addMusicToLibrary(musiclibrary,typeId);
		if(0==result){
			return ResultMessage.newFailure("向音乐库添加音乐失败");
		}
		return ResultMessage.newSuccess("向音乐库添加音乐成功").setData(result);
		
	}
	
	/**
	 * 从音乐库删除音乐
	 * @param musicId
	 * @author lixs
	 * @Date 2017.8.31
	 */
	@ResponseBody
    @RequestMapping(value = "/deleteMusicFromLibrary", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "从音乐库删除音乐", value = "从音乐库删除音乐")
	public ResultMessage<Integer> deleteMusicFromLibrary(@RequestBody Musiclibrary musiclibrary){
		logger.info("从音乐库删除音乐");
		
		int result = musiclibraryService.deleteMusicFromLibrary(musiclibrary);
		if(0==result){
			return ResultMessage.newFailure("从音乐库删除音乐失败");
		}
		return ResultMessage.newSuccess("从音乐库删除音乐成功").setData(result);
		
	}
	
	/**
	 * 更新音乐
	 * @param musiclibrary
	 * @author lixs
	 * @Date 2017.8.31
	 */
	@ResponseBody
    @RequestMapping(value = "/updateMusic", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "更新音乐信息", value = "更新音乐信息")
	public ResultMessage<Integer> updateMusic(@RequestBody Musiclibrary musiclibrary){
		logger.info("更新音乐");
		
		int result = musiclibraryService.updateMusic(musiclibrary);
		if(0==result){
			return ResultMessage.newFailure("更新音乐");
		}
		return ResultMessage.newSuccess("更新音乐").setData(result);
	}
	
	/**
	 * 多条件查询音乐信息
	 * @param typeId
	 * @param name
	 * @param createDateS
	 * @param createDateE
	 * @param offset
	 * @param total
	 * @author lixs
	 * @Date 2017.8.31
	 */
	@ResponseBody
    @RequestMapping(value = "/findMusicFromLibrary", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "多条件查询音乐信息", value = "多条件查询音乐信息")
	public ResultMessage<PageData> findMusicFromLibrary(String typeId,String name,String createDateS,String createDateE,
			   									 Integer offset,Integer limit){
		logger.info("多条件查询音乐信息");
		
		List<RecommendVo> musiclibrary = musiclibraryService.findMusicFromLibrary(typeId,name, createDateS, createDateE, offset, limit);
		
		PageData page = new PageData();
		page.setRows(musiclibrary);
		page.setTotal(musiclibraryService.findMusicFromLibraryForTotal(typeId,name, createDateS, createDateE));
		
		return ResultMessage.newSuccess("音乐查询成功").setData(page);
	}
	
	/**
	 * 查询所有音乐视频
	 * @author lixs
	 * @Date 2017.9.15
	 */
	@ResponseBody
    @RequestMapping(value = "/findVideoFromMusiclibrary", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "查询所有音乐视频", value = "查询所有音乐视频")
	public ResultMessage<List<Musiclibrary>> findVideoFromMusiclibrary(){
		
		logger.info("查询所有音乐视频");
		List<Musiclibrary> musiclibrary = musiclibraryService.findVideoFromMusiclibrary();
		if(null==musiclibrary){
			return ResultMessage.newFailure("未查到任何音乐视频");
		}
		return ResultMessage.newSuccess("查询所有音乐视频成功").setData(musiclibrary);
	}
	
	/**
	 * 根据歌手或者歌曲名查询音乐库中的音乐
	 * @param name
	 * @param offset
	 * @param limit
	 * @author lixs
	 * @Date 2017.10.23
	 */
	@ResponseBody
    @RequestMapping(value = "/findMusiclibraryByName", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "根据歌手或者歌曲名查询音乐库中的音乐", value = "根据歌手或者歌曲名查询音乐库中的音乐")
	public ResultMessage<PageData> findMusiclibraryByName(String name,Integer offset,Integer limit){
		logger.info("根据歌手或者歌曲名查询音乐库中的音乐");
		
		PageData page = new PageData();
		page.setRows(musiclibraryService.findMusiclibraryByName(name, offset, limit));
		page.setTotal(musiclibraryService.countMusiclibraryByName(name));
		
		return ResultMessage.newSuccess("根据歌手或者歌曲名查询音乐库中的音乐").setData(page);
		
		
	}
	
	/**
	 * 批量导入音乐(请勿乱用)
	 * @param vo
	 * @author lixs
	 * @Date 2017.11.13
	 */
	@ResponseBody
    @RequestMapping(value = "/addMusicToLibraryBatch", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "批量导入音乐(请勿乱用)", value = "批量导入音乐(请勿乱用)")
	public ResultMessage<Boolean>addMusicToLibraryBatch(@RequestBody BatchVo vo,String typeId){	
		List<MusicBatchVo> musicBatchVo = vo.getMusicBatchVo();
		
		Boolean result = musiclibraryService.addMusicToLibraryBatch(musicBatchVo, typeId);
		if(true==result){
			return ResultMessage.newSuccess("添加音乐成功");
		}
		return ResultMessage.newFailure("添加音乐失败");
		
	}
	
}
